package com.bootdo.common.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectRestriction.ReadOnly;
import com.bootdo.common.config.DbContextHolder;

/**
 * @author haojiangbo
 * @date 2018-12-5
 */
@Aspect
@Component
public class ReadOnlyInterceptor implements Ordered {
    private static final Logger log= LoggerFactory.getLogger(ReadOnlyInterceptor.class);

    /*@Around("@annotation(com.bootdo.common.annotation.DbRead) && (args(request,..) || args(..,request))")
    public Object setRead(ProceedingJoinPoint point,ReadOnly readOnly) throws Throwable{
        try{
            DbContextHolder.setDbType(DbContextHolder.READ);
            return point.proceed();
        }finally {
            DbContextHolder.clearDbType();
            log.info("清除threadLocal");
        }
    }
    */
    
    @Around("@annotation(com.bootdo.common.annotation.DbRead)")
    public Object setRead(ProceedingJoinPoint point) throws Throwable{
        try{
            DbContextHolder.setDbType(DbContextHolder.READ);
            return point.proceed();
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
        finally {
            DbContextHolder.clearDbType();
            log.info("清除threadLocal");
        }
    }
    
    
    @Override
    public int getOrder() {
        return 0;
    }
}